package com.suian.spring.thread;

import java.util.concurrent.locks.ReentrantLock;

public class T8seri {
    private static int x = 0, y = 0;
    private static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        int count = 0;
        while (true) {
            // 初始化变量
            x = 0;
            y = 0;
            a = 0;
            b = 0;

            // 创建并启动线程 1
            Thread thread1 = new Thread(() -> {
                a = 1;
                x = b;
            });

            // 创建并启动线程 2
            Thread thread2 = new Thread(() -> {
                b = 1;
                y = a;
            });

            thread1.start();
            thread2.start();

            // 等待两个线程执行完毕
            thread1.join();
            thread2.join();

            // 增加计数器
            count++;

            // 检查是否出现了指令重排序的结果
            if (x == 0 && y == 0) {
                System.out.println("第 " + count + " 次循环出现了指令重排序，x = " + x + ", y = " + y);
                break;
            }
        }


        ReentrantLock reentrantLock = new ReentrantLock();

    }
}
